package com.intelligent.mts.client.core.view;

import com.intelligent.mts.client.core.controller.MainController;
import com.intelligent.mts.client.core.modal.TestStationModal;
import com.intelligent.mts.client.utils.GBC;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

/**
 * Created by hao on 2016/10/12.
 */
@org.springframework.stereotype.Component
public class TestPanel extends JPanel{

    @Autowired
    MainController mainController;
    @PostConstruct
    private void initComponents() {
        inputPanel = new JPanel();
        inputPanel1 = new JPanel();
        showPanel = new JPanel();
        lbStationId = new JLabel();
        lbSnvalue = new JLabel();
        lbSFgvalue = new JLabel();
        btnSave = new JButton();
        stationId = new JComboBox();
        fgvalue = new JComboBox();
        snvalue = new JTextField();

        lbStationId.setText("Station id: ");
        lbSnvalue.setText("SerialNumber: ");
        lbSFgvalue.setText("FG PN: ");
        btnSave.setText("save");

        btnSave.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                btnSaveActionPerformed(e);
            }
        });

        stationId.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                stationIdcomboxChangeActionPerformed(e);
            }
        });


        inputPanel.setLayout(new GridBagLayout());
        inputPanel.add(lbStationId, new GBC(0, 0).setFill(GBC.HORIZONTAL).setWeight(5, 0).setInsets(5, 5, 5, 5));
        inputPanel.add(stationId, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(95, 0).setInsets(5, 5, 5, 5));
        inputPanel.add(lbSFgvalue, new GBC(0, 1).setFill(GBC.HORIZONTAL).setWeight(5, 0).setInsets(5, 5, 5, 5));
        inputPanel.add(fgvalue, new GBC(1, 1).setFill(GBC.HORIZONTAL).setWeight(95, 0).setInsets(5, 5, 5, 5));
        inputPanel.add(lbSnvalue, new GBC(0, 2).setFill(GBC.HORIZONTAL).setWeight(5, 0).setInsets(5, 5, 5, 5));
        inputPanel.add(snvalue, new GBC(1, 2).setFill(GBC.HORIZONTAL).setWeight(95, 0).setInsets(5, 5, 5, 5));
        inputPanel.add(btnSave, new GBC(1, 3).setAnchor(GBC.EAST).setInsets(5, 0, 5, 0));

        inputPanel1.setLayout(new GridBagLayout());
        inputPanel1.add(inputPanel, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(40, 100));
        inputPanel1.add(new JPanel(), new GBC(2, 0).setFill(GBC.HORIZONTAL).setWeight(60, 100));
        inputPanel1.setBorder(BorderFactory.createTitledBorder("Set Test Data"));


        //showPanel.setBorder(BorderFactory.createTitledBorder("TestStation Table"));
        showPanel.setLayout(new GridBagLayout());
        String columns[] = new String[]{"Station Id", "Production Id", "FG PN", "Serial Number"};
        testStationModal = new TestStationModal(columns);
        testStationTable = new JTable();
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
        tcr.setHorizontalAlignment(SwingConstants.CENTER);
        testStationTable.setDefaultRenderer(Object.class, tcr);
        scrollPane = new JScrollPane();

        scrollPane.setEnabled(false);
        testStationTable.setModel(testStationModal);
        testStationTable.getTableHeader().setVisible(true);
        testStationTable.getColumnModel().getColumn(0).setPreferredWidth(10);
        TableColumn column = null;
        for (int i = 0; i < testStationTable.getColumnCount(); i++) {
            column = testStationTable.getColumnModel().getColumn(i);
            String headName = column.getHeaderValue().toString();
            column.setPreferredWidth(50);
        }
        scrollPane.setViewportView(testStationTable);
        showPanel.add(scrollPane,new GBC(0,0).setFill(GBC.BOTH).setWeight(100,100));

        this.setLayout(new GridBagLayout());
        this.add(inputPanel1, new GBC(0, 0).setFill(GBC.HORIZONTAL).setAnchor(GBC.NORTH).setWeight(100,0));
        this.add(showPanel, new GBC(0,1).setFill(GBC.BOTH).setAnchor(GBC.NORTH).setWeight(100,50));
        this.setVisible(true);
    }

    private void btnSaveActionPerformed(ActionEvent event){
        String stationId = (String)getStationId().getSelectedItem();
        String fgValue = (String)getFgvalue().getSelectedItem();
        String sn = snvalue.getText().trim();
        if((StringUtils.isBlank(sn) || mainController.validateSn(sn)) && !mainController.findSerialNumberByStationId(stationId).equals(sn)){
            JOptionPane.showMessageDialog(btnSave, "SerialNumber repeats", "error",JOptionPane.WARNING_MESSAGE);
            return;
        }
        if (StringUtils.isBlank(stationId) || StringUtils.isBlank(fgValue)){
            JOptionPane.showMessageDialog(btnSave, "Plz choose Station id and FN PN", "error",JOptionPane.WARNING_MESSAGE);
            return;
        }
        if (!sn.matches("!\\p{ASCII}+!\\p{ASCII}+")){
            JOptionPane.showMessageDialog(btnSave, "Plz input sn as '!sn!pn'", "format error",JOptionPane.WARNING_MESSAGE);
            return;
        }
        mainController.createTestTrack(stationId);
        mainController.createTestSnTable(stationId, sn);
        mainController.createProduction(stationId, fgValue);
        reloadTable();
        JOptionPane.showMessageDialog(btnSave, "Save success", "success",JOptionPane.INFORMATION_MESSAGE);
    }

    public void initStationId(){
        java.util.List<String> list = new ArrayList<>();
        list = mainController.findAllTestStationId();
        if (list != null) {
            stationId.removeAllItems();
            for (String testStationId : list) {
                stationId.addItem(testStationId);
            }
        }
    }
    public void initFgvalue(){
        java.util.List<String> list =new ArrayList<>();
        list = mainController.findAllFGPN();
        if (list !=null && !list.isEmpty()){
            fgvalue.removeAllItems();
            for (String testFgValue : list) {
                fgvalue.addItem(testFgValue);
            }
        }
    }

    private void stationIdcomboxChangeActionPerformed(ItemEvent evt) {
        String StationId = (String) getStationId().getSelectedItem();
        initByStaionId(StationId);
    }

    private void initByStaionId(String StationId){
        String serialNumber = mainController.findSerialNumberByStationId(StationId);
        String fgpn = mainController.findFgpnByStationId(StationId);
        snvalue.setText(serialNumber);
        fgvalue.setSelectedItem(fgpn);
    }

    public void reloadTable() {
        testStationModal.setData();
        testStationTable.setModel(testStationModal);
    }

    public JComboBox getStationId() {
        return stationId;
    }

    public void setStationId(JComboBox stationId) {
        this.stationId = stationId;
    }

    public JComboBox getFgvalue() {
        return fgvalue;
    }

    public void setFGPN(JComboBox FGPN) {
        this.fgvalue = fgvalue;
    }

    private JPanel inputPanel;
    private JPanel inputPanel1;
    private JPanel showPanel;
    private JLabel lbStationId;
    private JLabel lbSnvalue;
    private JLabel lbSFgvalue;
    private JButton btnSave;
    private JComboBox stationId;
    private JComboBox fgvalue;
    private JTextField snvalue;

    private TestStationModal testStationModal;
    private JTable testStationTable;
    private JScrollPane scrollPane;
}
